package client

import (
	"easyblog/logicals"
	"easyblog/utils/errmsg"
	"github.com/beego/beego/v2/server/web"
)

type ArticleController struct {
	web.Controller
}

// @router /client/article/hot [get]
func (this *ArticleController) GetHotArticles() {

	articles, code := logicals.DefaultArticle.GetHotArticles()

	result := map[string]interface{}{
		"status":  code,
		"data":    articles,
		"message": errmsg.GetErrmsg(code),
	}
	this.Data["json"] = result
	this.ServeJSON() //返回json格式
}

// @router /client/articles [get]
func (this *ArticleController) GetPageArticles(){
	pageSize, err := this.GetInt("pagesize")
	if pageSize < 1 || err != nil{
		pageSize = 5
	}
	pageNum, err := this.GetInt("pagenum")
	if pageNum < 1 || err != nil{
		pageNum = 1
	}

	data, code, total := logicals.DefaultArticle.GetPageArticles(pageSize, pageNum)

	result := map[string]interface{}{
		"status":  code,
		"data":    data,
		"total": 	total,
		"message": errmsg.GetErrmsg(code),
	}
	this.Data["json"] = result
	this.ServeJSON() //返回json格式
}

// @router /client/article/:id [get]
func (this *ArticleController) GetOneArticle() {
	id, _ := this.GetInt(":id")
	article, code := logicals.DefaultArticle.GetArticleById(id)

	result := map[string]interface{}{
		"status":  code,
		"data":    article,
		"message": errmsg.GetErrmsg(code),
	}
	this.Data["json"] = result
	this.ServeJSON() //返回json格式
}
